from selenium import webdriver
from selenium.webdriver.common.by import By
# 页面操作
class PageLogin:
    # 构造方法：初始化操作
    def __init__(self):
        # 获取浏览器对象
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        self.driver.get('http://127.0.0.1:8000/')
    # 点击登录链接进入登录页
    def page_click_login_link(self):
        self.driver.find_element(By.LINK_TEXT, '登录').click()
    # 输入用户名
    def page_input_username(self, username):
        # 清空
        el = self.driver.find_element(By.CSS_SELECTOR, '#id_username')
        el.clear()
        el.send_keys(username)
    # 输入密码
    def page_input_password(self, password):
        self.driver.find_element(By.CSS_SELECTOR, '#id_password').send_keys(password)
    # 点击登录按钮
    def page_click_login_button(self):
        self.driver.find_element(By.CSS_SELECTOR, '[type="submit"]').click()
    # 组装
    def page_login(self, username, password):
        self.page_input_username(username)
        self.page_input_password(password)
        self.page_click_login_button()
    # 获取登录结果
    def page_get_result(self):
        return self.driver.find_element(By.CSS_SELECTOR, '.errornote').text
    # 关闭浏览器
    def page_close(self):
        self.driver.quit()
if __name__ == '__main__':
    # 通过类创建对象
    login = PageLogin()
    login.page_click_login_link()

    # 输入用户名，密码，点击登录
    login.page_login('admin', '2222222')
    print('结果：', login.page_get_result())
    from time import sleep
    sleep(3)
    login.page_close()